﻿using System;
using System.Collections.Generic;
using System.Text;

namespace WFL.Data
{
    class TLS
    {
        [ThreadStatic]
        private static TLS _current;

        public static TLS GetCurrent(bool initialize=true)
        {
            if (initialize
                && _current == null)
            {
                _current = new TLS();
            }
            return _current; 
        }



        private IDictionary<string, PrivateStorage> _privateData;

        public object PublicData;

        private TLS()
        {
            _privateData = new Dictionary<string, PrivateStorage>();
        }

        public PrivateStorage GetPrivateStorage(string name, bool initialize = true)
        {
            PrivateStorage storage = null;
            if (_privateData.ContainsKey(name))
            {
                storage = _privateData[name];
            }
            else
            {
                if (initialize)
                {
                    storage = new PrivateStorage();
                    _privateData[name] = storage;
                }
            }
            return storage;
        }

        public class PrivateStorage
        {
            public IConnectionProxy Connection;
            public object Other;
        }
    }
}
